const Router = require('koa-router');
const fetch = require('../common/httpClient');

let router = new Router();

router.get('current_introduction/:coin_name', async (ctx, next) => {
  let { coin_name } = ctx.params;

  let options = {
    url: `https://www.huobi.co/-/x/hb/p/api/contents/pro/currency_introduction?r=589qmp0uk4s&currency=${coin_name}&lang=zh-cn`,
    headers: {
      accept: 'application/json, text/plain, */*',
      'accept-encoding': 'gzip',
      'cache-control': 'no-cache',
      // cookie:
      //   '__cfduid=d365b7defe2c93b3169a78ebf94bd73da1541344609; gr_user_id=f03b34e3-efae-47b1-b5cf-74b5bc9066cb; _uab_collina=154134461717495378992576; grwng_uid=740272b9-362a-4d1d-a86d-ef537c84ddbb; _ga=GA1.2.1418137171.1541344625; __zlcmid=pQhnaSUbnSRpqZ; 8838a5745a973a12_gr_last_sent_cs1=F54CBA582041311C2A6DEE7F4DE9F5EC55B4F5CDE75CC655CCBD9D4234961990; _umdata=55F3A8BFC9C50DDA134C89938A88554F48116E67F39A7A7F975A509B302DD40C5AD10C5C0E9D4BA4CD43AD3E795C914C31A723453CD30868F974A165C96209A0; _gid=GA1.2.1829301847.1543846812; 8838a5745a973a12_gr_session_id=b6085a3e-57b4-4c4b-8ff8-7fe9cca2ed8c; 8838a5745a973a12_gr_session_id_b6085a3e-57b4-4c4b-8ff8-7fe9cca2ed8c=true; _gat_gtag_UA_108346576_1=1; SESSION=6e8d5a5d-0687-404e-869b-667ac161a7e9; ADRUM=s=1544442204466&r=https%3A%2F%2Fwww.huobi.co%2F%3F0; _gat=1',
      // ' hb-api-version': 1.1,
      pragma: 'no-cache',
      referer: ' https://www.huobi.co/zh-cn/coin_coin/exchange/',
      'user-agent':
        ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36}'
    }
  };
  let result = await fetch(options);
  ctx.body = JSON.parse(result.toString());
});
//https://huobi-info-api.huobiinfo.com/portal-gateway/search/listRelatedArticleIndexes?r=sculdbc1d0h&page=1&condition=btc&limit=10&t=1545384949923
//https://www.huobi.co/-/x/pro/v2/beta/common/symbols
router.get('articles/:coin_name', async (ctx, next) => {
  let { coin_name } = ctx.params;

  let options = {
    url: `https://huobi-info-api.huobiinfo.com/portal-gateway/search/listRelatedArticleIndexes?page=1&condition=${coin_name}&limit=10`,
    headers: {
      Accept: 'application/json, text/plain, */*',
      'Accept-Language': 'zh-CN',
      Origin: 'https://www.huobi.co',
      Referer: 'https://www.huobi.co/zh-cn/markets/my_market/',
      'User-Agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
    }
  };
  let result = await fetch(options);
  ctx.body = JSON.parse(result.toString());
});
router.get('symbols', async (ctx, next) => {
  let options = {
    url: 'https://www.huobi.co/-/x/pro/v2/beta/common/symbols',
    headers: {
      accept: 'application/json, text/plain, */*',
      'accept-encoding': 'gzip',
      'cache-control': 'no-cache',
      // cookie:
      //   '__cfduid=d365b7defe2c93b3169a78ebf94bd73da1541344609; gr_user_id=f03b34e3-efae-47b1-b5cf-74b5bc9066cb; _uab_collina=154134461717495378992576; grwng_uid=740272b9-362a-4d1d-a86d-ef537c84ddbb; _ga=GA1.2.1418137171.1541344625; __zlcmid=pQhnaSUbnSRpqZ; 8838a5745a973a12_gr_last_sent_cs1=F54CBA582041311C2A6DEE7F4DE9F5EC55B4F5CDE75CC655CCBD9D4234961990; _umdata=55F3A8BFC9C50DDA134C89938A88554F48116E67F39A7A7F975A509B302DD40C5AD10C5C0E9D4BA4CD43AD3E795C914C31A723453CD30868F974A165C96209A0; _gid=GA1.2.1829301847.1543846812; 8838a5745a973a12_gr_session_id=b6085a3e-57b4-4c4b-8ff8-7fe9cca2ed8c; 8838a5745a973a12_gr_session_id_b6085a3e-57b4-4c4b-8ff8-7fe9cca2ed8c=true; _gat_gtag_UA_108346576_1=1; SESSION=6e8d5a5d-0687-404e-869b-667ac161a7e9; ADRUM=s=1544442204466&r=https%3A%2F%2Fwww.huobi.co%2F%3F0; _gat=1',
      // ' hb-api-version': 1.1,
      pragma: 'no-cache',
      referer: 'https://www.huobi.co/zh-cn/markets/my_market/',
      'user-agent':
        ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36}'
    }
  };
  let result = await fetch(options);
  ctx.body = JSON.parse(result.toString());
});
router.post('buy', async ctx => {
  console.log('buy');
});
router.get('myassets', async ctx => {
  let result = (await ctx.db.select('sess_table', 'btc,usdt,mycoin,eth,ltc', {
    username: ctx.request.username
  }))[0];
  ctx.body = result;
});
module.exports = router.routes();
